package com.ruoyi.energy.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.QyEnterprise;
import com.ruoyi.energy.domain.vo.EnterpriseDetailVO;
import com.ruoyi.energy.domain.vo.EnterpriseExcelVO;
import com.ruoyi.energy.service.IQyEnterpriseService;
import com.ruoyi.omp.api.RemoteLeaseRecordService;
import com.ruoyi.omp.api.domain.FeLeaseRecord;
import com.ruoyi.omp.api.domain.vo.EnterpriseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 企业概况Controller
 *
 * @author cjie
 * @date 2021-08-02
 */
@Api(tags = " 企业概况")
@RestController
@RequestMapping("/qyEnterprise")
public class QyEnterpriseController extends BaseController {
    @Autowired
    private IQyEnterpriseService qyEnterpriseService;

    @Autowired
    private RemoteLeaseRecordService remoteLeaseRecordService;

    /**
     * 查询企业概况列表
     */
    @ApiOperation(value="小程序企业列表", notes="小程序企业列表")
    @GetMapping("/appList")
    public AjaxResult appList(QyEnterprise qyEnterprise) {
        startPage();
        List<QyEnterprise> list = qyEnterpriseService.selectQyEnterpriseList(qyEnterprise);
        return AjaxResult.success(list);
    }


    /**
     * 查询企业概况列表
     */
    @ApiOperation(value="小程序企业详情", notes="小程序企业详情")
    @GetMapping("/enterpriseDetail")
    public AjaxResult enterpriseDetail(String enterpriseId) {
        QyEnterprise qyEnterprise = qyEnterpriseService.selectQyEnterpriseById(enterpriseId);
        EnterpriseDetailVO detailVO = new EnterpriseDetailVO();
        BeanUtils.copyProperties(qyEnterprise, detailVO);
        //根据企业ID查询租赁记录
        R<List<FeLeaseRecord>> r = remoteLeaseRecordService.getRecordListByEnterpriseId(enterpriseId);
        if (r.getCode() == R.SUCCESS){
            detailVO.setRecordList(r.getData());
        }
        return AjaxResult.success(detailVO);
    }




    /**
     * 查询企业概况列表
     */
    @ApiOperation(value="查询企业概况列表", notes="查询企业概况列表")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:list")
    @GetMapping("/list")
    public TableDataInfo list(QyEnterprise qyEnterprise) {
        startPage();
        List<QyEnterprise> list = qyEnterpriseService.selectQyEnterpriseList(qyEnterprise);
        return getDataTable(list);
    }

    @ApiOperation(value="异步查询企业列表", notes="异步查询企业列表")
    @GetMapping("/listVO")
    public TableDataInfo listVO(QyEnterprise qyEnterprise) {
        startPage();
        List<QyEnterprise> list = qyEnterpriseService.selectQyEnterpriseList(qyEnterprise);
        return getDataTable(list);
    }
        /**
         * 导出企业概况列表
         */
    @ApiOperation(value="导出企业概况列表", notes="导出企业概况列表")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:export")
    @Log(title = "企业概况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QyEnterprise qyEnterprise) throws IOException {
        List<QyEnterprise> list = qyEnterpriseService.selectQyEnterpriseList(qyEnterprise);
        ExcelUtil<QyEnterprise> util = new ExcelUtil<QyEnterprise>(QyEnterprise.class);
        util.exportExcel(response, list, "企业概况数据");
    }
    /**
     * 获取企业概况详细信息
     */
    @ApiOperation(value="获取企业概况详细信息", notes="获取企业概况详细信息")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(qyEnterpriseService.selectQyEnterpriseById(id));
    }

    @GetMapping( value = "/getEenterprise/{id}")
    R<EnterpriseVo> getEenterprise(@PathVariable("id") String id){
        QyEnterprise qyEnterprise = qyEnterpriseService.selectQyEnterpriseById(id);
        EnterpriseVo result = new EnterpriseVo();
        BeanUtils.copyProperties(qyEnterprise,result);
        return R.ok(result);
    }

    @GetMapping( value = "/name/{name}")
    R<String> getEenterpriseByName(@PathVariable("name") String name){
        return R.ok(qyEnterpriseService.selectQyEnterpriseByName(name));
    }

    /**
     * 企业入驻
     */
    @ApiOperation(value="企业入驻", notes="企业入驻")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:add")
    @Log(title = "企业入驻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QyEnterprise qyEnterprise) {
        return toAjax(qyEnterpriseService.insertQyEnterprise(qyEnterprise));
    }

    /**
     * 远程接口插入
     */
    @PostMapping(value = "/add")
    public R romoteAdd(@RequestBody QyEnterprise qyEnterprise) {
        return toR(qyEnterpriseService.insertSimpleQyEnterprise(qyEnterprise));
    }

    /**
     * 修改企业概况
     */
    @ApiOperation(value="修改企业概况", notes="修改企业概况")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:edit")
    @Log(title = "企业概况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QyEnterprise qyEnterprise) {
        return toAjax(qyEnterpriseService.updateQyEnterprise(qyEnterprise));
    }

    /**
     * 删除企业概况
     */
    @ApiOperation(value="删除企业概况", notes="删除企业概况")
    @ApiImplicitParam(name = "ids", value = "企业概况ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:remove")
    @Log(title = "企业概况", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(qyEnterpriseService.deleteQyEnterpriseByIds(ids));
    }



    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<EnterpriseExcelVO> util = new ExcelUtil<>(EnterpriseExcelVO.class);
        util.importTemplateExcel(response, "企业租赁信息");
    }

    /**
     * 导入抄表记录列表,
     */
    @ApiOperation(value = "导入房源数据", notes = "导入房源数据")
    @PreAuthorize(hasPermi = "energy:qyEnterprise:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<EnterpriseExcelVO> util = new ExcelUtil<>(EnterpriseExcelVO.class);
        List<EnterpriseExcelVO> enterpriseExcelVOS = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = qyEnterpriseService.importData(enterpriseExcelVOS);
        return AjaxResult.success(message);

    }








}
